package com.bing.hello;

import com.bing.task.v2.SanBiaoTask;
import com.bing.task.v2.SanBiaoWriteTask;
import com.bing.task.v2.UplanTask;
import com.bing.task.v2.UplanWriteTask;
import com.bing.util.Constant;
import com.bing.util.Env;
import com.bing.util.RenRen;
import com.bing.vo.ReserveRecord;
import com.bing.vo.SanBiao;
import com.bing.vo.UPlan;
import org.ini4j.Ini;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.util.ResourceUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by Administrator on 2015/2/3 0003.
 */
public class HelloRenRen {


    public static Map<String, String> cookies = null;

    @BeforeClass
    public static void beforeClass() {
        cookies = RenRen.getCookies(Constant.USER_NAME, Constant.PASS_WORD);
    }

    @Test
    public void test() throws IOException {
        Map<String, String> map = new HashMap<String, String>();
        map.put("j_username", "13148408117");
        map.put("j_password", "@123.com11");
        map.put("targetUrl", "http://www.renrendai.com/");

        Connection.Response response = Jsoup.connect(Constant.LOGIN_URL)
                .data(map).userAgent(Constant.USERAGENT)
                .method(Connection.Method.POST)
                .timeout(Constant.TIMEOUT)
                .execute();
        if (response.statusCode() == 200) {
            System.out.println(response.body());
            Document document = Jsoup.connect("http://www.renrendai.com/lend/detailPage.action?loanId=454770").
                    timeout(Constant.TIMEOUT).
                    cookies(response.cookies())//这个就是上面获取的cookies
                    .get();
            System.out.println(document.outerHtml());
        }
    }

    @Test
    public void testUplan() {
        Document document = RenRen.getHtml(Constant.listPlan + "79", cookies);
        String title = document.select("#plan-basic-panel .ui-box-title h3").text();
        String planAmount = document.select(".planinfo .mb25 dl:eq(0) em").text();
        String planIncome = document.select(".planinfo .mb25 dl:eq(1) em").text();
        String endDate = document.select(".planinfo .mb25 dl:eq(2) em").text();
        String danBao = document.select("#plan-basic-products").attr("data-products");
        String planInfo = document.select(".planinfo ul:eq(1)").text(); //投标范围 保障方式 本金+利息  锁定结束 2013年12月29日 加入上限 1,000,000元
        String v1 = document.select("h'di'ai'd").get(0).text();
        String k1 = document.select(".ui-box-white-bg .plan-status span").get(0).text();
        String v2 = document.select(".ui-box-white-bg .plan-status em").get(1).text();
        String k2 = document.select(".ui-box-white-bg .plan-status span").get(1).text();
        List<Element> keys = document.select("#plan-tab-content [data-name=intro] th");
        List<Element> values = document.select("#plan-tab-content [data-name=intro] td");
        Map<String, String> planIntro = new HashMap<String, String>();
        for (int i = 0; i < keys.size(); i++) {
            planIntro.put(keys.get(i).text(), values.get(i).text());
        }
        String question = document.select(".question_ul").text();
        System.out.println(question);
    }

    @Test
    public void testSanBiao() {
        Document document = RenRen.getHtml(Constant.sanBiao_url + "2", cookies);

        Map<String, Object> data;

        String title = document.select(".ui-box-title h3").text();
        String biao = document.select(".ui-box-title h3").attr("title");
        List<Element> key1 = document.select(".loaninfo div dl");
        List<Element> value1 = document.select(".loaninfo div dd");
        Map<String, Object> detail = new HashMap<String, Object>();
        for (int i = 0; i < key1.size(); i++) {
            detail.put(key1.get(i).text(), value1.get(i).text());
        }
        RenRen.extraKeyValue(document, detail, ".loaninfo  ul li span"); //标信息
        RenRen.extraKeyValue(document, detail, ".ui-tab-content-info .ui-tab-content-expediente ul li "); //信用档案

        String status = document.select(".stamp em").attr("class");
        detail.put("状态", status);
        RenRen.extraKeyValue(document, detail, ".ui-tab-content-info .ui-tab-content-basic ul li"); //用户信息
        String fangChang = document.select(".ui-tab-content-info .ui-tab-content-basic ul li span:matches(房.*产) ~ span span:has(i.icon-check-checked)").text();
        String fangDai = document.select(".ui-tab-content-info .ui-tab-content-basic ul li span:matches(房.*贷) ~ span span:has(i.icon-check-checked)").text();
        String cheChang = document.select(".ui-tab-content-info .ui-tab-content-basic ul li span:matches(车.*产) ~ span span:has(i.icon-check-checked)").text();
        String cheDai = document.select(".ui-tab-content-info .ui-tab-content-basic ul li span:matches(车.*贷) ~ span span:has(i.icon-check-checked)").text();
        String sex = document.select(".ui-icon-gender-male, .ui-icon-gender-female").attr("title");
        String uid = document.select(".ui-tab-content-info .ui-tab-content-basic h4 span").text().replace("ID: ", "");
        String xinyongFen = document.select(".ui-tab-content-info .ui-tab-content-expediente h4 span").text();
        String desc = document.select(".ui-tab-content-description div").text();
        detail.put("房       产", fangChang);
        detail.put("房       贷", fangDai);
        detail.put("车       产", cheChang);
        detail.put("车       贷", cheDai);
        detail.put("性别", sex);
        detail.put("借款人Id:", uid);
        detail.put("信用分数", xinyongFen);
        detail.put("借款描述", desc);

        Map<String, Object> touBiao = RenRen.getJson(Constant.touBiao_url + 1, Map.class);
        data = (Map<String, Object>) touBiao.get("data");
        List<Map<String, Object>> lenderRecords = (List<Map<String, Object>>) data.get("lenderRecords");
        System.out.println(lenderRecords);


        Map<String, Object> zaiQuan = RenRen.getJson(Constant.zaiQuan_url + 1, Map.class);
        data = (Map<String, Object>) zaiQuan.get("data");
        List<Map<String, Object>> lenders = (List<Map<String, Object>>) data.get("lenders");

        RenRen.getCredit(document);
        System.out.println(lenders);

    }

    @Test
    public void testHuanKuan() {

        List<Map<String, Object>> huanKuan = RenRen.getMapListData(Constant.haiKuan_url + 35, "phases");
        System.out.println(huanKuan);

    }

    @Test
    public void testReserveRecord() {
        ReserveRecord reserveRecord = RenRen.getJson(Constant.reserveRecord_Url + "66", ReserveRecord.class);
        System.out.println(reserveRecord);
    }

    @Test
    public void formatTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", new Locale("US"));
        System.out.println(sdf.format(new Date()));
    }

    @Test
    public void testResult() {
        ReserveRecord reserveRecord = RenRen.getJson(Constant.reserveRecord_Url + "66", ReserveRecord.class);
        System.out.println(reserveRecord);
    }

    @Test
    public void testResult2() {
        Map<String, Object> reserveRecord = RenRen.getJson(Constant.result_Url + "66", Map.class);
        Map<String, Object> data = (Map<String, Object>) reserveRecord.get("data");
        List<Object> financePlanVos = (List<Object>) data.get("financePlanVos");
        Map<String, Object> res = (Map<String, Object>) financePlanVos.get(0);
        System.out.println(reserveRecord);
    }

    @Test
    public void testUplanAll() throws Exception {
        Callable<UPlan> uPlanCallable = new UplanTask(1, cookies);
        UPlan uPlan = uPlanCallable.call();
        UplanWriteTask uplanWriteTask = new UplanWriteTask(uPlan, Env.get().getPath());
        uplanWriteTask.run();

    }

    @Test
    public void testZhuangRan() {
        for (int i = 1; ; i++) {
            System.out.println("--" + i);
            List<Map<String, Object>> loanTransferLogList = RenRen.getMapListData(Constant.zhuangRan_url + i, "loanTransferLogList");
            if (loanTransferLogList.size() > 0) {
                System.out.println("有数据:  " + i);
            }
        }
    }


    @Test
    public void testSanBiaoAll() throws Exception {
        Callable<SanBiao> sanBiaoCallable = new SanBiaoTask(488, cookies, 0);
        SanBiao sanBiao = sanBiaoCallable.call();
        SanBiaoWriteTask sanBiaoWriteTask = new SanBiaoWriteTask(sanBiao, Env.get().getPath());
        sanBiaoWriteTask.run();
    }

    @Test
    public void testZaiQuanAll() throws Exception {
        Callable<SanBiao> sanBiaoCallable = new SanBiaoTask(188, cookies, 1);
        SanBiao sanBiao = sanBiaoCallable.call();
        SanBiaoWriteTask sanBiaoWriteTask = new SanBiaoWriteTask(sanBiao, Env.get().getPath());
        sanBiaoWriteTask.run();
    }

    @Test
    public void testReadIni() throws IOException {

        Ini ini = new Ini(ResourceUtils.getFile("classpath:fetch.ini"));
        Ini.Section section = ini.get("Uplan");
        assertEquals("1", section.get("start"));
        assertTrue(185 == ini.get("Uplan", "end", Integer.class).intValue());
    }


}
